package condominio.servlet.reserva;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import condominio.excepcion.DAOExcepcion;
import condominio.modelo.Reserva;
import condominio.modelo.Usuario;
import condominio.negocio.GestionReservas;

/**
 * Servlet implementation class ReservaListarServlet
 */
@WebServlet("/ReservaListarServlet")
public class ReservaListarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReservaListarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("Dentro de doPost del servlet ReservaListarSerlvet");
		
		SimpleDateFormat parse = new SimpleDateFormat("dd-MM-yyyy");
		
		String strinicioreserva=request.getParameter("inicio");
		java.util.Date dateinicioreserva=null;
		try {
			dateinicioreserva = parse.parse(strinicioreserva);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		java.sql.Date inicioreserva = new java.sql.Date(dateinicioreserva.getTime());
		
		String strfinreserva=request.getParameter("fin");
		java.util.Date datefinreserva=null;
		try {
			datefinreserva = parse.parse(strfinreserva);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		java.sql.Date finreserva = new java.sql.Date(datefinreserva.getTime());
		System.out.println("Dentro de doPost del servlet ReservaListarSerlvet 2");
		HttpSession session = request.getSession();
		Usuario usuario_actual = (Usuario)session.getAttribute("USUARIO_ACTUAL");
		
		String residente = usuario_actual.getResidente();
		System.out.println("Dentro de doPost del servlet ReservaListarSerlvet 3");
		GestionReservas negocio = new GestionReservas();
		Collection<Reserva> lista = null;
		try {
			// Guardar en el ambiente de request
			lista = negocio.listar(residente, inicioreserva, finreserva);
			request.setAttribute("reservas", lista);
			RequestDispatcher rd = request
					.getRequestDispatcher("Reservas.jsp");
			rd.forward(request, response);

		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("Error.jsp");
			rd.forward(request, response);
		}
		
	}

}
